<template>
  <el-dialog :visible.sync="isshow" :title="title" @close="closeEvent">
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model.trim="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model.trim="form.code" />
      </el-form-item>
      <el-form-item
        label="部门负责人"
        prop="manager"
      ><el-select v-model="form.manager" @focus="selectBlur">
        <el-option
          v-for="i in userList"
          :key="i.id"
          :label="i.username"
          :value="i.username"
        />
      </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="delevent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {
  sysUserSimple,
  companyDepartmentPost,
  companyDepartmentPut
} from '@/api/department'
export default {
  props: {
    initTreeData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      mode: '',
      itemData: [],
      userList: [],
      isshow: false,
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const temArr = this.initTreeData.filter((item) => {
                if (this.mode === 'add') {
                  return item.pid === this.itemData.id
                } else if (this.mode === 'edit') {
                  return (
                    item.pid === this.itemData.pid &&
                    item.id !== this.itemData.id
                  )
                }
              })
              const bol = temArr.some((item) => {
                return item.name === value
              })
              bol ? callback(new Error('部门名称重名:' + value)) : callback()
            }
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (this.mode === 'add') {
                const bol = this.initTreeData.some((item) => {
                  return item.code === value
                })
                bol ? callback(new Error('部门编码重复')) : callback()
              }
              if (this.mode === 'edit') {
                const bol = this.initTreeData.some((item) => {
                  return item.code === value && item.id !== this.itemData.id
                })
                bol ? callback(new Error('部门编码重复')) : callback()
              }
            }
          }
        ],
        manager: [{ required: true, message: '必填', trigger: 'change' }],
        introduce: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  computed: {
    title() {
      return {
        add: '新增部门',
        edit: '编辑部门'
      }[this.mode]
    }
  },
  // watch: {
  //   isshow(newVal) {
  //     if (!newVal) {
  //       this.form = {
  //         name: '',
  //         code: '',
  //         manager: '',
  //         introduce: '',
  //         pid: ''
  //       }
  //       this.$nextTick()
  //       this.$refs.form.resetFields()
  //     }
  //   }
  // },
  mounted() {
    this.$bus.$on('addEvent', (flag, item, mode) => {
      this.isshow = flag
      this.itemData = item
      this.form.pid = item.id
      this.mode = mode
      if (mode === 'edit') {
        this.form = JSON.parse(JSON.stringify(item))
      }
    })
  },
  beforeDestroy() {
    this.$bus.$off('addEvent')
  },
  methods: {
    delevent() {
      this.isshow = false
    },
    closeEvent() {
      this.isshow = false
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
      this.$refs.form.resetFields()
    },
    async selectBlur() {
      if (!this.userList.length) {
        const res = await sysUserSimple()
        this.userList = res.data
      }
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            await companyDepartmentPost(this.form)
            this.$message.success('新增成功')
          } else if (this.mode === 'edit') {
            await companyDepartmentPut(this.form)
            this.$message.success('修改成功')
          }
          this.isshow = false
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style></style>
